package com.jbit.springboot04.util;

import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

public class RedisUtil {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    public void set(String key,Object value){
        redisTemplate.opsForValue().set(key,value);
    }

    public Object get(String key){
        return redisTemplate.opsForValue().get(key);
    }

    /**
     *
     * @param key
     * @param value
     * @param ttl 单位 秒
     */
    public void set(String key,Object value,long ttl){
        redisTemplate.opsForValue().set(key,value,ttl, TimeUnit.SECONDS);
    }

    public boolean exists(String key){
        return redisTemplate.hasKey(key);
    }
    public void del(String key){
        redisTemplate.delete(key);
    }

    public void expire(String key,long ttl){
        redisTemplate.expire(key,ttl,TimeUnit.SECONDS);
    }

}
